/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.locationbuilding.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.locationbuilding.param.*;
import vip.xiaonuo.biz.modular.locationbuilding.vo.BizLocationBuildingResp;
import vip.xiaonuo.biz.modular.relation.entity.BizRelation;
import vip.xiaonuo.biz.modular.relation.enums.BizRelationCategoryEnum;
import vip.xiaonuo.biz.modular.relation.service.BizRelationService;
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.biz.modular.locationbuilding.entity.BizLocationBuilding;
import vip.xiaonuo.biz.modular.locationbuilding.mapper.BizLocationBuildingMapper;
import vip.xiaonuo.biz.modular.locationbuilding.service.BizLocationBuildingService;

import java.util.List;

/**
 * 楼宇Service接口实现类
 *
 * @author kongxm
 * @date 2022/11/06 21:07
 **/
@Service
public class BizLocationBuildingServiceImpl extends ServiceImpl<BizLocationBuildingMapper, BizLocationBuilding> implements BizLocationBuildingService {
    @Autowired
    private BizRelationService bizRelationService;

    @Override
    public Page<BizLocationBuildingResp> page(BizLocationBuildingPageParam bizLocationBuildingPageParam) {
        return getBaseMapper()
                .page(Page.of(bizLocationBuildingPageParam.getCurrent(), bizLocationBuildingPageParam.getSize()),
                        bizLocationBuildingPageParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizLocationBuildingAddParam bizLocationBuildingAddParam) {
        // It can't be executed if there already exist a same name or a same code.
        long existSameCodeCount = count(new LambdaQueryWrapper<BizLocationBuilding>()
                .eq(BizLocationBuilding::getCode, bizLocationBuildingAddParam.getCode()));
        Assert.isTrue(existSameCodeCount == 0, "编号{}已存在", bizLocationBuildingAddParam.getCode());

        long existSameNameCount = count(new LambdaQueryWrapper<BizLocationBuilding>()
                .eq(BizLocationBuilding::getName, bizLocationBuildingAddParam.getName()));
        Assert.isTrue(existSameNameCount == 0, "名称{}已存在", bizLocationBuildingAddParam.getName());


        BizLocationBuilding bizLocationBuilding = BeanUtil.toBean(bizLocationBuildingAddParam, BizLocationBuilding.class);
        this.save(bizLocationBuilding);
        bizRelationService.saveRelationWithClear(bizLocationBuilding.getId(),
                bizLocationBuildingAddParam.getUserId(),
                BizRelationCategoryEnum.BIZ_RELATION_BUILDING_USER_IN_CHARGE.getValue());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizLocationBuildingEditParam bizLocationBuildingEditParam) {
        // It can't be executed if there already exist a same name or a same code.
        long existSameCodeCount = count(new LambdaQueryWrapper<BizLocationBuilding>()
                .eq(BizLocationBuilding::getCode, bizLocationBuildingEditParam.getCode())
                .ne(BizLocationBuilding::getId, bizLocationBuildingEditParam.getId()));
        Assert.isTrue(existSameCodeCount == 0, "编号{}已存在", bizLocationBuildingEditParam.getCode());

        long existSameNameCount = count(new LambdaQueryWrapper<BizLocationBuilding>()
                .eq(BizLocationBuilding::getName, bizLocationBuildingEditParam.getName())
                .ne(BizLocationBuilding::getId, bizLocationBuildingEditParam.getId()));
        Assert.isTrue(existSameNameCount == 0, "名称{}已存在", bizLocationBuildingEditParam.getName());


        BizLocationBuilding bizLocationBuilding = this.queryEntity(bizLocationBuildingEditParam.getId());
        BeanUtil.copyProperties(bizLocationBuildingEditParam, bizLocationBuilding);
        this.updateById(bizLocationBuilding);
        bizRelationService.saveRelationWithClear(bizLocationBuilding.getId(),
                bizLocationBuildingEditParam.getUserId(),
                BizRelationCategoryEnum.BIZ_RELATION_BUILDING_USER_IN_CHARGE.getValue());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizLocationBuildingIdParam> bizLocationBuildingIdParamList) {
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(bizLocationBuildingIdParamList, BizLocationBuildingIdParam::getId));
        bizLocationBuildingIdParamList.forEach(item -> bizRelationService.remove(new LambdaQueryWrapper<BizRelation>()
                .eq(BizRelation::getObjectId, item.getId())));
    }

    @Override
    public BizLocationBuilding detail(BizLocationBuildingIdParam bizLocationBuildingIdParam) {
        return this.queryEntity(bizLocationBuildingIdParam.getId());
    }

    @Override
    public BizLocationBuilding queryEntity(String id) {
        BizLocationBuilding bizLocationBuilding = this.getById(id);
        if (ObjectUtil.isEmpty(bizLocationBuilding)) {
            throw new CommonException("楼宇不存在，id值为：{}", id);
        }
        return bizLocationBuilding;
    }

    @Override
    public List<BizLocationBuilding> list(BizLocationBuildingListParam bizLocationBuildingListParam) {
        BizLocationBuilding bizLocationBuilding = BeanUtil.copyProperties(bizLocationBuildingListParam, BizLocationBuilding.class);
        return list(new LambdaQueryWrapper<>(bizLocationBuilding)
                .eq(BizLocationBuilding::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE));
    }
}
